﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ares.Engine
{
    public class BackupListComparer
    {
        public List<BackupEntry> DoComparison(List<BackupEntry> Source, List<BackupEntry> Dest)
        {
            List<BackupEntry> BackupEntries = new List<BackupEntry>();

            IEnumerable<BackupEntry> EnumSource = Source;
            IEnumerable<BackupEntry> EnumDest = Dest;

            //Maybe this can be done with linq one day...

            //Compare the source tree
            List<BackupEntry> tempDest = new List<BackupEntry>();

            //Go through the Source
            foreach (BackupEntry tmp in Source)
            {
                //Check, if the original file still exists
                BackupEntry tmpEntry = Dest.Find(x => x.Path == tmp.Path);
                if (tmpEntry == null)
                {
                    //It doesn't exist, so set it to removed
                    tempDest.Add(new BackupEntry { Path = tmp.Path, Hash = tmp.Hash, State = BackupEntryState.Removed });
                }
                else
                {
                    //It exists. But maybe, it has changed?
                    if (tmpEntry.Hash != tmp.Hash)
                    {
                        //Well, it has changed
                        tempDest.Add(new BackupEntry { Path = tmpEntry.Path, Hash = tmpEntry.Hash, State = BackupEntryState.Changed });
                    }
                }

                //Now, remove it!
                Dest.Remove(tmpEntry);
            }

            //Add the other files to the templist
            tempDest.AddRange(Dest);

            return tempDest;
        }
    }
}
